require 'View/UIBase/UIBase'

UIDoubleAward = UIBase.New(PanelResNames.UIDoubleAward)

function UIDoubleAward.Init(func, id)
    UIDoubleAward.activeId = id;
    UIBase.Init(UIDoubleAward,func);
end

-- 创建回调
function UIDoubleAward:OnCreate()
    self.countDownNum = 0;
    self.bg = self:GetChild("bg");
    self.bgText = self:GetChild("bgText");
    self.titleTop = self:GetChild("titleTop");
    self.titleText = self:GetChild("titleText");
    self.goBtn = self:GetChild("go");
    self.tipTime = self:GetChild("tipTime");
    self.tipDecs2 = self:GetChild("tipDecs2");
    self.overController = self:GetController("IsOver");
    self:addEvent();
end

-- 显示
function UIDoubleAward:OnEnable()
    if self.activeId == nil then
        self:OnBack();
    else
        self:Refresh();
    end
end

function UIDoubleAward:addEvent()
    self:RegisterOnClick(self:GetChild("bgClose"),self.OnClose);
    self:RegisterOnClick(self:GetChild("go"),self.OnGo);
    self:RegisterEvent(Config.EventType.Fresh_Active,self.Refresh);
end

function UIDoubleAward:removeEvent()
end

function UIDoubleAward:Refresh()
    self:UpdateData();
end

function UIDoubleAward:UpdateData()
    self.tableData, self.serverData = logicMgr.ActiveManager.getActiveData(self.activeId);
    if self.tableData == nil or self.serverData == nil or not self.serverData.isShow then
        self:OnBack();
        return ;
    end
    logicMgr.ActiveManager.updateActiveOpenStatus(self.activeId);
    self.bg.url = tostring(self.tableData.f_BgPic);
    self.bgText.url = tostring(self.tableData.f_HeadPic);
    self.titleTop.text = getLanguage(self.tableData.f_Title);
    self.titleText.text = getLanguage(self.tableData.f_TitleDesc);
    self.countDownNum = self.serverData.endTime - HelperFunc.GetServerTime();
    self:RemoveTimeCall(self.OnCountDown);
    if self.countDownNum <= 0 then
        self.overController.selectedIndex = 0;
        self.tipDecs2.text = "活动已结束";
    else
        self.overController.selectedIndex = 1;
        self:TimeCall(self.OnCountDown, 1000, -1);
        self.tipTime.text = HelperFunc.GetSurpTimeStr(self.countDownNum);
    end
end

--- 倒计时
function UIDoubleAward:OnCountDown()
    self.countDownNum = self.countDownNum - 1;
    self.tipTime.text = HelperFunc.GetSurpTimeStr(self.countDownNum);
    if self.countDownNum <= 0 then
        self.overController.selectedIndex = 0;
        self.tipDecs2.text = "活动已结束";
    end
end

--- 领取
function UIDoubleAward:OnGo()
    if logicMgr.FuncOpenManager.isOpen(self.tableData.f_Switchover, true) then
        self:OnClose();
        if isOpenUI(PanelNames.UIActivity) then
            closeUI(PanelNames.UIActivity);
        end
        logicMgr.FuncOpenManager.jump(self.tableData.f_Switchover, true);
    end
end

-- 关闭
function UIDoubleAward:OnClose()
    self:OnBack();
    self:RemoveAllTimeCall();
    logicMgr.ActiveManager.showPopUpTips();
end

-- 隐藏
function UIDoubleAward:OnDisable()
end

-- 卸载
function UIDoubleAward:OnDestroy()
    self:removeEvent();
end

return UIDoubleAward